#ifndef TYPES_H
#define TYPES_H

#include "sysdef.h"

template <size_t n> struct __regtype__{typedef int int_r; typedef unsigned int uint_r; };
template <> struct __regtype__<8>     {typedef long long int_r;typedef unsigned long long uint_r;};
typedef __regtype__<sizeof(void*)>::int_r  int_r;
typedef __regtype__<sizeof(void*)>::uint_r uint_r;

typedef uint_r				PILOADDRESS;

#if defined(WIN32) || defined(WINDOWS)
typedef unsigned                THREAD_ROUTINE_RETURN_TYPE;

#else
typedef unsigned char                   BYTE;
typedef BYTE*                           LPBYTE;
typedef unsigned short		WORD;
typedef unsigned int		DWORD;
typedef char				INT8;
typedef short				INT16;
typedef int					INT32;
typedef unsigned char		UINT8;
typedef unsigned short		UINT16;
typedef unsigned int		UINT32;
typedef int					INT;
typedef	unsigned int		UINT;
typedef char				CHAR;
typedef unsigned char		UCHAR;
typedef long				LONG;
typedef unsigned long		ULONG;
typedef short				SHORT;
typedef	unsigned short		USHORT;
typedef int					BOOL;
typedef float				FLOAT;
typedef long long			INT64;
typedef unsigned long long	UINT64;
typedef double				DOUBLE;
typedef void *				HANDLE;
typedef void *				HMODULE;
typedef void				VOID;
typedef int					SOCKET;
typedef const char*			LPCTSTR;
typedef void*				PVOID;
typedef void*				LPVOID;
typedef void*				PVOID;
typedef void*				LPVOID;

#if defined(_UNICODE) || defined(UNICODE)
typedef wchar_t TCHAR;  
#else
typedef char TCHAR;
#endif

typedef void*               THREAD_ROUTINE_RETURN_TYPE;


#endif



template <typename T> struct TPoint
{
	T x;
	T y;
};

template <typename T> struct TRectangle
{
	T left;
	T top;
	T right;
	T bottom;
};


// template for comparisons
template <typename T> 
class TLess
{
private:
    typedef T value_type;

public:
    BOOL operator () (const value_type& a, const value_type& b) const
    {
        return (a) < (b);
    }
};

template <typename T> 
class TIntCompare
{
public:
    int operator () (const T& a, const T& b) const
    {
        return (int)a - (int)b;
    }
};

template <typename T> 
class TUIntCompare
{
public:
    int operator () (const T& a, const T& b) const
    {
        if((DWORD)a < (DWORD)b) return -1;
        else if((DWORD)b < (DWORD)a) return 1;
        else return 0;
    }
};

template <typename T> 
class TInt64Compare
{
public:
    int operator () (const T& a, const T& b) const
    {
        return (int)((INT64)a - (INT64)b);
    }
};

template <typename T> 
class TUInt64Compare
{
public:
    int operator () (const T& a, const T& b) const
    {
        if((UINT64)a < (UINT64)b) return -1;
        else if((UINT64)b < (UINT64)a) return 1;
        else return 0;
    }
};

//used to compare 2 variables, return positive for a>b, negetive for a<b, zero for equal
template < typename T,typename L=TLess<T> > 
class TLessCompare
{
public:
    L _lessComparison;
    int operator () (const T& a, const T& b) const
    {
        if(_lessComparison(a, b)) return -1;
        else if(_lessComparison(b,a)) return 1;
        else return 0;
    }

protected:
    

};


typedef enum {
    EnumTriStateNA = -1,
    EnumTriStateNo = 0,
    EnumTriStateYes =1
} TriState;

typedef enum {
	EnumCompareResultLess = -1,
	EnumCompareResultEqual = 0,
	EnumCompareResultGreater = 1,
	EnumCompareResultLeftNull =0xF0,
	EnumCompareResultRightNull =0xF,
	EnumCompareResultBothNull = 0xFF,

} compare_result_t;

template <typename T> struct _invalid_object_
{
    static T& get() { static T v; return v; }
};

/**
	* get default value of type T
	* the default value for number type is 0
	* the default value for pointer type is NULL
	* the default value for class object will use the default constructor
	* usage:
	*      int x = _type_default_value_<int>::get();
	*
	* use this technique to initialize a non-existing node within KMapByVector
	*
	*/
template <typename T> struct _type_default_value_
{
	static const T& get() { static T v; return v; }
};
template <typename T> struct _type_default_value_<T*>
{
	static T* get() { return NULL; }
};
template <> struct _type_default_value_<void*>
{
	static void* get() { return NULL; }
};
template <> struct _type_default_value_<char>
{
	static char get() { return 0; }
};
template <> struct _type_default_value_<unsigned char>
{
	static int get() { return 0; }
};
template <> struct _type_default_value_<short>
{
	static short get() { return 0; }
};
template <> struct _type_default_value_<unsigned short>
{
	static unsigned short get() { return 0; }
};
template <> struct _type_default_value_<int>
{
	static int get() { return 0; }
};
template <> struct _type_default_value_<unsigned int>
{
	static int get() { return 0; }
};
template <> struct _type_default_value_<float>
{
	static float get() { return 0.0; }
};
template <> struct _type_default_value_<double>
{
	static double get() { return 0.0; }
};
template <> struct _type_default_value_<long long>
{
	static long long get() { return 0; }
};
template <> struct _type_default_value_<unsigned long long>
{
	static unsigned long long get() { return 0; }
};

// to judge is a object simple type
template <typename T> struct _is_simple_type_
{
    static const bool value = sizeof(T)<=sizeof(void*) ? true:false;
};
template <> struct _is_simple_type_<INT64>
{
    static const bool value = true;
};
template <> struct _is_simple_type_<UINT64>
{
    static const bool value = true;
};
template <> struct _is_simple_type_<double>
{
    static const bool value = true;
};

#endif
